<template>
  <div class="profile-app-container">
    <!-- 顶部导航 -->
    <el-header class="profile-header">
      <div class="header-title">我的</div>
    </el-header>

    <!-- 主要内容区域 - 占满剩余空间 -->
    <main class="profile-main-content">
      <!-- 用户信息卡片 -->
      <div class="user-card">
        <router-link :to="userId ? '/userinfo' : '/login'" class="user-link">
          <div class="user-avatar">
            <el-avatar :size="60" :icon="User" v-if="!userAvatar" />
            <el-avatar :size="60" :src="userAvatar" v-else />
          </div>
          <div class="user-details">
            <div class="user-name">{{ username || '登录/注册' }}</div>
            <div class="user-phone">
              <el-icon><Iphone /></el-icon>
              <span>{{ phone || '暂无绑定手机号' }}</span>
            </div>
          </div>
          <el-icon class="arrow-icon"><ArrowRight /></el-icon>
        </router-link>
      </div>

      <!-- 数据统计区域 -->
      <div class="stats-section">
        <div class="stat-item">
          <div class="stat-value">0.00</div>
          <div class="stat-label">我的余额</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">0</div>
          <div class="stat-label">我的优惠</div>
        </div>
        <div class="stat-item">
          <div class="stat-value">0</div>
          <div class="stat-label">我的积分</div>
        </div>
      </div>

      <!-- 功能菜单区域 -->
      <!-- 功能列表 -->
      <section class="menu-section">
        <div class="menu-item" @click="goToOrder">
          <el-icon class="menu-icon"><KnifeFork /> /></el-icon>
          <span class="menu-text">我的订单</span>
          <el-icon class="menu-arrow"><ArrowRight /></el-icon>
        </div>

        <div class="menu-item">
          <el-icon class="menu-icon"><Coin /></el-icon>
          <span class="menu-text">积分商城</span>
          <el-icon class="menu-arrow"><ArrowRight /></el-icon>
        </div>

        <div class="menu-item">
          <el-icon class="menu-icon"><Star /></el-icon>
          <span class="menu-text">外卖会员卡</span>
          <el-icon class="menu-arrow"><ArrowRight /></el-icon>
        </div>
      </section>

      <section class="menu-section">
        <div class="menu-item">
          <el-icon class="menu-icon"><Service /></el-icon>
          <span class="menu-text">服务中心</span>
          <el-icon class="menu-arrow"><ArrowRight /></el-icon>
        </div>
      </section>

      <!-- 退出登录按钮 -->
      <div class="logout-section" v-if="userId">
        <el-button type="danger" class="logout-btn" @click="handleLogout">退出登录</el-button>
      </div>
    </main>

    <!-- 底部导航 -->
    <FooterGuide />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useCookies } from 'vue3-cookies'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  User,
  Iphone,
  ArrowRight,
  Tickets,
  Star,
  Service
} from '@element-plus/icons-vue'
import FooterGuide from '@/components/FooterGuide/FooterGuide.vue'

const { cookies } = useCookies()
const router = useRouter()

// 用户数据
const userId = ref(cookies.get('userId') || '')
const username = ref(cookies.get('username') || '')
const phone = ref(cookies.get('phone') || '')
const userAvatar = ref(cookies.get('avatar') || '')
// 跳转到订单页面
const goToOrder = () => {
  router.push('/Order')
}
// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确认退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 清除cookie
    cookies.remove('token')
    cookies.remove('userId')
    cookies.remove('username')
    cookies.remove('phone')
    cookies.remove('avatar')

    // 重置用户数据
    userId.value = ''
    username.value = ''
    phone.value = ''
    userAvatar.value = ''

    ElMessage.success('退出成功')
    router.push('/login')
  }).catch(() => {
    // 取消退出
  })
}
</script>

<style scoped>
.profile-app-container {
  display: flex;
  flex-direction: column;
  height: 100vh; /* 占满整个视口高度 */
  width: 100vw; /* 占满整个视口宽度 */
  background-color: #f5f5f5;
}

.profile-header {
  background-color: #02a774;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  color: white;
  font-size: 18px;
  font-weight: bold;
  flex-shrink: 0; /* 防止header被压缩 */
}

.profile-main-content {
  flex: 1; /* 占据剩余所有空间 */
  overflow-y: auto; /* 允许内容滚动 */
  padding-bottom: 60px; /* 为底部导航留出空间 */
}

.user-card {
  background-color: #02a774;
  padding: 20px 15px;
}

.user-link {
  display: flex;
  align-items: center;
  color: white;
  text-decoration: none;
}

.user-avatar {
  margin-right: 15px;
}

.user-details {
  flex: 1;
}

.user-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.user-phone {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.user-phone .el-icon {
  margin-right: 5px;
}

.arrow-icon {
  font-size: 18px;
}

.stats-section {
  display: flex;
  background-color: white;
  padding: 15px 0;
  margin-bottom: 10px;
}

.stat-item {
  flex: 1;
  text-align: center;
  border-right: 1px solid #f1f1f1;
}

.stat-item:last-child {
  border-right: none;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #ff5f3e;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.menu-section {
  background-color: white;
  margin-bottom: 10px;
}

.menu-title {
  padding: 15px;
  font-size: 16px;
  font-weight: bold;
  border-bottom: 1px solid #f5f5f5;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f5f5f5;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item .el-icon:first-child {
  margin-right: 15px;
  color: #02a774;
  font-size: 20px;
}

.menu-item span {
  flex: 1;
  font-size: 16px;
  color: #333;
}

.menu-item .el-icon:last-child {
  color: #999;
}

.logout-section {
  padding: 15px;
}

.logout-btn {
  width: 100%;
}
</style>